//
//  ZYMainViewController.m
//  YaYa
//
//  Created by 张亚 on 2019/1/8.
//  Copyright © 2019年 张亚. All rights reserved.
//

#import "ZYMainViewController.h"
#import "ZYMainTopView.h"
#import <AFNetworking.h>
@interface ZYMainViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *contentScrollView;
@property (nonatomic, strong) NSArray *datalist;
@property (nonatomic, strong) ZYMainTopView * topView;

@end

@implementation ZYMainViewController

- (ZYMainTopView *)topView {
    if (_topView == nil) {
        _topView = [[ZYMainTopView alloc] initWithFrame:CGRectMake(0, 0, 200, 50) titleNames:self.datalist];
        WS(weakSelf);
        _topView.topBlock = ^(NSInteger tag) {
            CGPoint point = CGPointMake(tag * SCREEN_WIDTH, weakSelf.contentScrollView.contentOffset.y);
            [weakSelf.contentScrollView setContentOffset:point animated:YES];
            
        };
    }
    return _topView;
}
- (NSArray *)datalist {
    if (_datalist == nil) {
        _datalist = @[@"关注",@"热门",@"附近"];
    }
    return _datalist;
}

- (UIScrollView *)contentScrollView {
    if (_contentScrollView == nil) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-kTabbarHeight)];
        _contentScrollView.delegate = self;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.bounces = NO;
        [self.view addSubview:_contentScrollView];
    }
    return _contentScrollView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = self.topView;
    
    [self setupChildViewControllers];
    
    
    NSString * urlPath = [NSString stringWithFormat:@"http://169.254.15.155:9999/app/loginProperty"];
    AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
    sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
//    sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/html",@"text/plain",@"application/x-www-form-urlencoded",nil];

    

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = @"123123";
    params[@"password"] = @"123123";
    
    [sessionManager POST:urlPath parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"] ;
        NSString *errorStr = [[ NSString alloc ] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",errorStr);
    }];
    
    
    
    
}

- (void)setupChildViewControllers {
    
    NSArray * vcName = @[@"ZYFocuseViewController",@"ZYHotViewController",@"ZYNearViewController"];
    for (int i = 0; i < vcName.count; i++) {
        UIViewController * vc = [[NSClassFromString(vcName[i]) alloc] init];
        vc.title = self.datalist[i];
        //这个只是将vc添加到self上，不会执行viewdidload
        [self addChildViewController:vc];
        
    }
    
    //将子控制器的view,加到mainVCe的view上
    
    //设置scrollview的contentSize
    self.contentScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * self.datalist.count, 0);
    
    //默认进入第一个子控制器
    [self scrollViewDidEndScrollingAnimation:self.contentScrollView];
}



- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSLog(@"%f",scrollView.contentOffset.x);
    CGFloat width = SCREEN_WIDTH;
    CGFloat height = SCREEN_HEIGHT;
    CGFloat offset = scrollView.contentOffset.x;
    //获取索引值
    NSInteger idx = offset / width;
    //联动标题
    [self.topView scrolling:idx];
    //根据索引值获取控制器
    UIViewController * vc = self.childViewControllers[idx];
    //当前vc是否执行过
    if ([vc isViewLoaded]) return;
    
    vc.view.frame = CGRectMake(offset, 0, scrollView.frame.size.width, height);
    
    [scrollView addSubview:vc.view];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self scrollViewDidEndScrollingAnimation:scrollView];
}

@end
